package ems.dev.easycode.service.builder.impl;

import cn.hutool.core.util.StrUtil;
import ems.dev.easycode.common.FreemarkerEntity;
import ems.dev.easycode.domain.entity.TableEntity;
import ems.dev.easycode.service.builder.ServiceBuilder;
import ems.dev.easycode.service.entity.MethodEntity;
import ems.dev.easycode.service.entity.ServiceEntity;
import ems.dev.easycode.util.*;
import java.sql.SQLException;

/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/5/26
 * @function 业务层构建器
 */
public class ServiceBuilderImpl implements ServiceBuilder {

    /**
     * 构建简单的增删改查和初始化方法
     * @param schema
     * @param table
     */
    @Override
    public void builderSimpleCRUDMethods(String schema, String table) throws SQLException {
        TableEntity tableEntity = Db2Util.getTableInfo(schema,table);
        FreemarkerEntity config = new FreemarkerEntity();
        config.setTemplateName("service.ftl");
        String generateLocation = PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME, Constant.EASYCODE_GERENATE_LOCATION);
        config.setOutputPath(StrUtil.isBlank(generateLocation)?"C:\\":generateLocation);//不配置默认生成到c盘
        config.setOutputName("Service"+MappingUtil.getPageNo(table)+".java");

        ServiceEntity serviceEntity = new ServiceEntity();
        serviceEntity.setInitLoad(generateInitLoad(schema, table));
        config.setDataSource(serviceEntity);
        FreemarkerUtil.generateFinalFile(config);
    }

    private MethodEntity generateInitLoad(String schema, String table) {
        MethodEntity methodEntity = new MethodEntity();
        StringBuilder sb = new StringBuilder();
        String pageNo = MappingUtil.getPageNo(table);
        methodEntity.setServiceName("Service"+pageNo);
        methodEntity.setJavaCode(sb.toString());
        return methodEntity;
    }

    private MethodEntity generateQueryMethod(String schema, String table){
        //todo
        return null;
    }

    private MethodEntity generateDeleteMethod(String schema, String table){
        //todo
        return null;
    }

    private MethodEntity generateInsertMethod(String schema, String table){
        //todo
        return null;
    }

    private MethodEntity generateUpdateMethod(String schema, String table){
        //todo
        return null;
    }

}
